package com.trusfort.redis;

import redis.clients.jedis.*;
import redis.clients.jedis.params.ScanParams;
import redis.clients.jedis.resps.ScanResult;

import java.util.*;

public class RedisDirectService implements RedisService{

    private static RedisDirectService service;

    public static RedisDirectService init(String hostAndPort,String passwd){
        if (service==null){
            service = new RedisDirectService();
        }
        
        String [] hostPort = hostAndPort.split(":");
        
        RedisConnectionPool.getInstance(hostPort[0],Integer.valueOf(hostPort[1]),passwd);
        
        return service;
    }

    private  Jedis getJedis(Integer dbIndex){
        Jedis jedis = null;
        try {
            jedis = RedisConnectionPool.getInstance().getJedis(dbIndex);
        } catch (Exception e) {
            //log.error("{}",e.getMessage(), e);
        }
        return jedis;
    }
    private static void returnResource(Jedis jedis){
        RedisConnectionPool.getInstance().returnResource(jedis);
    }

              

    @Override
    public void close() {
        // dont need to close
    }

    @Override
    public long ttl(String key) throws Exception {
        return new JedisClient().ttl(key);
    }

	@Override
	public ScanResult<String> scan(Integer dbIndex, String key, String cursor, Integer count) throws Exception {
		Jedis jedis = null;
		try {
			jedis = RedisConnectionPool.getInstance().getJedis(dbIndex);

			ScanParams scanParams=new ScanParams();
			scanParams.count(count);
			scanParams.match(key);
			
			//使用sscan命令获取500条数据，使用cursor游标记录位置，下次循环使用
			return jedis.scan(cursor, scanParams);

		} catch (Exception e) {
			e.printStackTrace();
//			log.error("{}", ERR_MSG, e);
			return null;
		} finally {
			RedisConnectionPool.getInstance().returnResource(jedis);
		}
	}

	@Override
	public void set(String key, String value) throws Exception {
		Jedis jedis = null;
		try {
			jedis = RedisConnectionPool.getInstance().getJedis(0);

			jedis.set(key,value);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			RedisConnectionPool.getInstance().returnResource(jedis);
		}
		
	}

	@Override
	public void set(Integer dbIndex, String key, String value) throws Exception {
		Jedis jedis = null;
		try {
			jedis = RedisConnectionPool.getInstance().getJedis(dbIndex);

			jedis.set(key,value);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			RedisConnectionPool.getInstance().returnResource(jedis);
		}
		
	}

	@Override
	public void set(String key, String value, int seconds) throws Exception {
		Jedis jedis = null;
		try {
			jedis = RedisConnectionPool.getInstance().getJedis(0);

			jedis.setex(key,seconds,value);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			RedisConnectionPool.getInstance().returnResource(jedis);
		}
	}
	
	
	@Override
	public void set(List<String> list, int seconds) throws Exception {
		Jedis jedis = null;
		try {
			jedis = RedisConnectionPool.getInstance().getJedis(0);

			// 开启pipeline
		    Pipeline pipeline = jedis.pipelined();
		    
		    // 添加多个命令到pipeline
		    for(String str:list) {
		    	String [] kv = str.split(";");
		    	pipeline.setex(kv[0],seconds,kv[1]);	
		    }
		    
		    // 执行所有命令
		    pipeline.sync();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			RedisConnectionPool.getInstance().returnResource(jedis);
		}
	}
	
	
	
	@Override
	public void del(Integer dbIndex,List<String> list) throws Exception {
		Jedis jedis = null;
		try {
			jedis = RedisConnectionPool.getInstance().getJedis(dbIndex);

			// 开启pipeline
		    Pipeline pipeline = jedis.pipelined();
		    
		    // 添加多个命令到pipeline
		    for(String str:list) {
		    	pipeline.del(str);	
		    }
		    
		    // 执行所有命令
		    pipeline.sync();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			RedisConnectionPool.getInstance().returnResource(jedis);
		}
	}
	
	
	

	@Override
	public void set(Integer dbIndex, String key, String value, int seconds) throws Exception {
		Jedis jedis = null;
		try {
			jedis = RedisConnectionPool.getInstance().getJedis(dbIndex);

			jedis.setex(key,seconds,value);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			RedisConnectionPool.getInstance().returnResource(jedis);
		}
		
	}

}
